package com.unity.bh3.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.unity.bh3.model.FriendMessage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface FriendMessageMapper extends BaseMapper<FriendMessage> {

    @Select({"<script>",
        "SELECT * FROM friend_message WHERE (sender_id = #{userId} AND receiver_id = #{friendId})",
        "OR (sender_id = #{friendId} AND receiver_id = #{userId})",
        "ORDER BY created_time ASC",
        "</script>"})
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "senderId", column = "sender_id"),
        @Result(property = "receiverId", column = "receiver_id"),
        @Result(property = "content", column = "content"),
        @Result(property = "status", column = "status"),
        @Result(property = "createdTime", column = "created_time")
    })
    List<FriendMessage> getHistoryMessages(Long userId, Long friendId);

    @Update({"<script>",
        "UPDATE friend_message SET status = 1 WHERE (sender_id = #{userId} AND receiver_id = #{friendId})",
        "OR (sender_id = #{friendId} AND receiver_id = #{userId})",
        "</script>"})
    void markMessagesAsRead(Long userId, Long friendId);
}
